package com.codeplex.springsurv.businessLogic;

import com.codeplex.springsurv.*;
import com.codeplex.springsurv.controllers.UserController;
import com.codeplex.springsurv.dataaccess.mappers.SurveysMapper;
import com.codeplex.springsurv.dataaccess.mappers.UsersMapper;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SurveyService {

	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);

	@Resource(name = "userMapper")
	private UsersMapper userMapper;
	@Resource(name = "surveyMapper")
	private SurveysMapper surveyMapper;

	// Gets a specific survey by it's Id.
	public Survey getSurvById(Long pId) {
		Survey srv = null;
		try {
			srv = this.surveyMapper.getSurveyById(pId);
			srv.setUsrUser(this.userMapper.getUserByNick(this.surveyMapper.getSurvUserNickBySurvId(pId)));
			srv.setLstQuestions(this.getQuestListBySurvId(srv.getI64Id()));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return srv;
	}

	// Given a survey Id, gets its list of questions.
	protected List<Question> getQuestListBySurvId(Long pId) {
		List<Question> lstQuest = null;
		try {
			lstQuest = this.surveyMapper.getQuestionListBySurvId(pId);
			for (int i = 0; i < lstQuest.size(); i++) {
				if (lstQuest.get(i) != null) {
					lstQuest.get(i)
							.setLstAnswers(
									this.getAnsListByQuestId(lstQuest.get(i)
											.getI64Id()));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lstQuest;
	}

	// Given a question Id, gets its list of answers.
	protected List<Answer> getAnsListByQuestId(Long pId) {
		List<Answer> lstAns = null;
		try {
			lstAns = this.surveyMapper.getAnswerListByQuestId(pId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lstAns;
	}

	// Registers a new survey in the system.
	// Receives as parameter a survey. Inserts the new survey in the
	// system and call the appropriate method in order to insert the
	// questions stored in its list.
	public void insertNewSurvey(Survey pSurv) throws SQLException {
		pSurv.setI64Id(this.surveyMapper.getLastSurvId() + 1);
		this.surveyMapper.insertSurvey(pSurv);
		for (int i = 0; i < pSurv.getLstQuestions().size(); i++) {
			if (pSurv.getLstQuestions().get(i) != null) {
				this.insertNewQuestion(pSurv.getLstQuestions().get(i),
						pSurv.getI64Id());
			}
		}
	}

	// Registers a new question in the system.
	// Receives as parameters the question to be inserted and the Id
	// of the survey to which the question is associated to.
	protected void insertNewQuestion(Question pQuest, Long pSId)
			throws SQLException {
		pQuest.setI64Id(this.surveyMapper.getLastQuestId() + 1);
		this.surveyMapper.insertQuestion(pQuest, pSId);
		for (int i = 0; i < pQuest.getLstAnswers().size(); i++) {
			if (pQuest.getLstAnswers().get(i) != null) {
				this.insertNewAnswer(pQuest.getLstAnswers().get(i),
						pQuest.getI64Id());
			}
		}
	}

	// Registers a new answer in the system.
	// Receives as parameters the answer to be inserted and the Id of
	// the question to which the answer is associated to.
	protected void insertNewAnswer(Answer pAns, Long pQId) throws SQLException {
		pAns.setI64Id(this.surveyMapper.getLastAnswId() + 1);
		this.surveyMapper.insertAnswer(pAns, pQId);
	}

	// Receives as parameter a User and gets a list of surveys not filled or
	// created by the user.
	public List<Survey> getSurvLstNotFilledByUser(User pUser)
			throws SQLException {
		return this.surveyMapper.getSurveysListNotFilledByUser(pUser
				.getStrNick());
	}
	
	// Receives as parameter a User and gets a list of surveys created by the user.
		public List<Survey> getSurvLstCreatedByUser(User pUser)
				throws SQLException {
			return this.surveyMapper.getSurveysListByUserCreator(pUser
					.getStrNick());
		}

	// Changes the state of a survey to inactive.
	public void closeSurvey(Long pSurvId) throws SQLException {
		this.surveyMapper.closeSurvey(pSurvId);
	}

	// Receives as parameter an instance of UserSurvey containing the answers
	// chosen by the user who filled the survey, the identity of that survey
	// and the nickname of user who filled it.
	public void insertFilledSurvey(UserSurvey pUsSurv) throws SQLException {
		List<Answer> lstAns = pUsSurv.getLstSelAnswers();
		for (int i = 0; i < lstAns.size(); i++) {
			this.surveyMapper.insertFilledSurvey(pUsSurv.getUsrUser()
					.getStrNick(), pUsSurv.getSrvSurvey().getI64Id(), lstAns
					.get(i).getI64Id());
		}
	}

	// Receives as parameter the Id of a given question and returns the percentage of users
	// that chose each answer.
	public List<String> getStatsByQuestId(Long pQuestId) throws SQLException {
		List<String> lstStats = new ArrayList<String>();
		List<Answer> lstAns = this.surveyMapper
				.getAnswerListByQuestId(pQuestId);
		Integer quantUs = this.surveyMapper.getQuantUsersBySurvId(this.surveyMapper.getSurvIdByQuestId(pQuestId));
		for (int i = 0; i < lstAns.size(); i++) {
			Integer quant = this.surveyMapper.getQuantByAnsId(lstAns.get(i)
					.getI64Id());
			String aux = ">> " + (quant * 100 / quantUs) + "% Contestaron: '" + lstAns.get(i).getStrAnswer() + "'.";
			lstStats.add(aux);
		}
		return lstStats;

	}
}
